const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    imgUrls: [
      'https://mall.s.maizuo.com/768e6ce3267e815ff65c42c18048a1af.jpg?x-oss-process=image/resize,w_563',
      'https://mall.s.maizuo.com/955be78098ebe9cdc0bfe13619d30e7e.jpg?x-oss-process=image/resize,w_563',
      'https://mall.s.maizuo.com/00fb5c0a41c436610f22738d487f2b70.jpg?x-oss-process=image/resize,w_563'
    ],
    indicatorDots: false,
    autoplay: false,
    _id: '',
    ani:{},//飞入购物车动画的对象
    cartNum:0

  },

  onLoad: function(options) {
    let {
      _id
    } = options;
    this.setData({
      _id: _id || '5cf65d1f44f87c016cf3d034'
    })

    this.getDetail(this.data._id);
  },

  getDetail: function(_id) {
    let url = '/product/detail?_id='+_id;
    app.$http.get(url).then(res=> {
      console.log(res);
      this.setData({
        detail: res.detail,
        cartNum: res.detail.cartNum 
        
      })
     
    })
  },

onShow(){
  var animation = wx.createAnimation({
  });

  animation.opacity(0).step()
  this.setData({
    ani: animation.export()
  })
},
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  // 分类显示与隐藏
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  imgNumber: function(e) {
    this.setData({

    })
  },

  // 加入购物车
  addCart() {
    let _id = this.data._id;
    let url = '/cart/add?_id='+_id;
    app.$http.get(url).then(res=> {
      console.log(res);
      console.log(res.num)
     
    })


    // 定义动画时间和形式
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    });
    // 动画过程
    animation.translate(-300 / 750 * wx.getSystemInfoSync().windowWidth, 0)
    .opacity(1).scale(0.5).step({ duration:500})
    animation.translate(0, 0).opacity(0).scale(1).step({ duration: 30 })
    this.setData({
      ani: animation.export()
    })

    this.setData({
      cartNum: this.data.cartNum + 1
    })
    


  }
})